package com.example.smartcity.ui.Adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Parcelable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.smartcity.R;
import com.example.smartcity.ui.Bean.patientsCardItemBean;
import com.example.smartcity.ui.dashboard.SetPaMessage;
import com.example.smartcity.ui.dashboard.addPersionCard;

import java.util.ArrayList;
import java.util.List;

public class PatientsCardAdapter extends ArrayAdapter<patientsCardItemBean.RowsBean> {
    private int resource;
    private Context context;
    private List<patientsCardItemBean.RowsBean> objects;
    public PatientsCardAdapter(@NonNull Context context, int resource, @NonNull List<patientsCardItemBean.RowsBean> objects) {
        super(context, resource, objects);
        this.resource = resource;
        this.context = context;
        this.objects = objects;
    }
    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        patientsCardItemBean.RowsBean item = getItem(position);
        View view = null;
        ViewHolder viewHolder = null;
        if (convertView==null){
            view = LayoutInflater.from(getContext()).inflate(resource,parent,false);
            viewHolder = new ViewHolder();
            viewHolder.TV_userCardID = view.findViewById(R.id.TV_userCardID);
            viewHolder.TV_userNName = view.findViewById(R.id.TV_userNName);
            viewHolder.TV_userTel = view.findViewById(R.id.TV_userTel);
            viewHolder.IV_setCard = view.findViewById(R.id.IV_setCard);
            view.setTag(viewHolder);
        }else{
            view = convertView;
            viewHolder = (ViewHolder) view.getTag();
        }
        viewHolder.TV_userTel.setText(item.getTel());
        viewHolder.TV_userNName.setText(item.getName());
        viewHolder.TV_userCardID.setText(item.getCardId());
        viewHolder.IV_setCard.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, SetPaMessage.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.putExtra("CName",objects.get(position).getName());
                intent.putExtra("CSex",objects.get(position).getSex()-1);
                intent.putExtra("CCardId",objects.get(position).getCardId());
                intent.putExtra("CBirthday",objects.get(position).getBirthday());
                intent.putExtra("CuserPhone",objects.get(position).getTel());
                intent.putExtra("CAdd",objects.get(position).getAdders());
                intent.putExtra("cardId",objects.get(position).getId());
                context.startActivity(intent);
            }
        });
        return view;
    }
     static class ViewHolder{
        TextView TV_userNName;
        TextView TV_userCardID;
        TextView TV_userTel;
        ImageView IV_setCard;
    }
}
